package com.msb.monitor.task;

import com.msb.common.constart.CacheConstant;
import com.msb.monitor.feign.CacheClient;
import com.msb.monitor.util.MailUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.Set;

/**
 * @Author: lmf
 * @Create: 2025/3/24 16:19
 * @module
 */
@Component
public class MonitorClientBalanceTask {


    @Resource
    private MailUtil mailUtil;

    @Resource
    private CacheClient cacheClient;

    @XxlJob("monitorClientBalance")
    public void monitorClientBalance() throws MessagingException {
        // 获取redis中所有客户余额
        Set<String> keys = cacheClient.keys(CacheConstant.CLIENT_BALANCE + "*");
        // 客户余额限制
        long CLIENT_BALANCE_LIMIT = 100000;

        // 对客户余额小于 CLIENT_BALANCE_LIMIT 发送邮件推送
        for (String key : keys) {
            Long balance = cacheClient.hGetToLong(key, "balance");
            if (balance < CLIENT_BALANCE_LIMIT) {
                String text = "您的当前余额为%s,为避免短信无法发送请及时充值";
                mailUtil.sendEmail("余额不足", String.format(text, balance / 1000));
            }
        }


        System.out.println("monitorClientBalance");
    }
}
